jsx3.require("tibco.admin.enterpriseAssets.EnterpriseAssetsTabInterface");

if(!jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.subVars.SubVars")) {
    jsx3.require("com.gix.gui.Block")

    /**
     * subVars
     */
    jsx3.lang.Class.defineClass("tibco.admin.enterpriseAssets.subVars.SubVars",
            com.gix.gui.Block,
            [tibco.admin.enterpriseAssets.EnterpriseAssetsTabInterface],
            function (subvars) {

				subvars.prototype.isEditmode = false;

                subvars.prototype.init = function() {
                    this.jsxsuper();
                };

                subvars.prototype.isLoaded = function(msg) {
                    if(msg.status == "SUCCESS") {
                    }
                }

                subvars.prototype.loadDetails = function() {
                    try{
						this.loadBtnStatus();
						this.getDescendantOfName("selMatrix_56L").setDefaultText(tibco.admin.Main.getServer().getDynamicProperty("@@SelDefaultText_56L"));
                        var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/enterpriseAssets/subVars/rules/getSvarsInEnterprise.xml", "getSvarsInEnterprise", "Get Substitution Variables");
						objService.setEndpointURL(tibco.admin.util.ServiceHelper.getSubstitutionVarsEndpointURL());
						objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onCallGetSvarsInEnterpriseSuccess); 
						objService.doCall();
						this.getDescendantOfName("btnDel_56L").setEnabled(0, true);
                    }
                    catch(e) {
                        tibco.admin.util.Log.ENVIRONMENT.error("Error in tibco.admin.enterpriseAssets.subVars.SubVars#loadDetails: " + e)
                    }
                }

                subvars.prototype.onCallGetSvarsInEnterpriseSuccess = function(objEvent){
                    this.getDescendantOfName("mtxDetail_56L").repaint();
					if(this.getDescendantOfName("mtxDetail_56L").getSortedIds().length == 0){
						this.getDescendantOfName("blkNullLabel_56L").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
					}else{
						this.getDescendantOfName("blkNullLabel_56L").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
					}
                }

				/**
				 * switch between view mode and edit mode, change the buttons display status and matrix column template value
				 */
				subvars.prototype.loadBtnStatus = function(){
					if(this.isEditmode){
						this.getDescendantOfName("btnSave_56L").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
						this.getDescendantOfName("btnCancel_56L").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
						this.getDescendantOfName("btnEdit_56L").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
						this.getDescendantOfName("blkNullLabel_56L").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
						this.getDescendantOfName("mcType_56L").setValueTemplate(tibco.admin.Main.getServer().getDynamicProperty("@@SubVarsTypeColumnValueTemp"));
					}else{
						this.getDescendantOfName("btnSave_56L").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
						this.getDescendantOfName("btnCancel_56L").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
						this.getDescendantOfName("btnEdit_56L").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
						this.getDescendantOfName("mcType_56L").setValueTemplate("");
					}
				}

				subvars.prototype.callBtnAddOnclick = function() {
					this.isEditmode = true;
					this.pluginObj.beginEditMode();
					this.loadBtnStatus();
					this.addMatrixRecord();
                }
				
				subvars.prototype.callBtnDelOnclick = function() {
					var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/enterpriseAssets/subVars/rules/deleteSvarsInEnterprise.xml", "deleteSvarsInEnterprise" , tibco.admin.Main.getServer().getDynamicProperty("@@deleteSvarsInEnterprise_56L"));
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getSubstitutionVarsEndpointURL());
					objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onDeleteSvarsInEnterpriseSuccess);
                    objService.doCall();
                }

				subvars.prototype.callBtnEditOnclick = function() {
					this.isEditmode = true;
					this.pluginObj.beginEditMode();
					this.loadBtnStatus();
					this.addMatrixRecord();
                }

				subvars.prototype.callBtnSaveOnclick = function() {
					var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/enterpriseAssets/subVars/rules/addSvarsToEnterprise.xml", "addSvarsToEnterprise", tibco.admin.Main.getServer().getDynamicProperty("@@addSvarsToEnterprise_56L"));
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getSubstitutionVarsEndpointURL());
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onAddSvarsToEnterpraiseSuccess);
                    objService.doCall();
                }

				subvars.prototype.callBtnCancelOnclick = function() {
					this.isEditmode = false; 
					this.pluginObj.endEditMode();
					this.loadDetails();
                }
				
				subvars.prototype.callBtnRefreshOnclick = function() {
					this.isEditmode = false; 
					this.pluginObj.endEditMode();
					this.loadDetails();
                }
				
				
				subvars.prototype.addMatrixRecord = function() {
					var record = this.getDescendantOfName("mtxDetail_56L").insertRecord({jsxid:jsx3.xml.CDF.getKey()},null,false);
					record.setAttribute("isNewNode","true");
					this.getDescendantOfName("mtxDetail_56L").repaint();
					this.getDescendantOfName("mtxDetail_56L").focusCellById(record.getAttribute("jsxid"),"McName_56L");
					this.getDescendantOfName("btnDel_56L").setEnabled(1, true); 
                }
				
				subvars.prototype.onMatrixSelected = function() {
					if(this.getDescendantOfName("mtxDetail_56L").getSelectedIds().length > 0){
						this.getDescendantOfName("btnDel_56L").setEnabled(1, true); 
					}else{
						this.getDescendantOfName("btnDel_56L").setEnabled(0, true); 
					}
                }

				/**
				 * Judge the Matrix cell whether or not change to edit mode.
				 */
				subvars.prototype.isChagetoEditMode = function(blkObj,columnObj,strRECORDID,objEvent) {
					if((blkObj.getRecordNode(strRECORDID).getAttribute("isNewNode") == "true" || columnObj.getDisplayIndex() == 2 )&& this.isEditmode)
					{
						return true;
					}else{
						return false;
					}
				};
				
				subvars.prototype.onAddSvarsToEnterpraiseSuccess = function(objEvent) {
					this.isEditmode = false;
					this.pluginObj.endEditMode();
					this.loadDetails();
                };
				
				subvars.prototype.onDeleteSvarsInEnterpriseSuccess = function(objEvent) {
					this.pluginObj.endEditMode();
					this.getDescendantOfName("btnDel_56L").setEnabled(0, true); 
					this.loadDetails();
                };

				subvars.prototype.onAfterEdit = function(blkObj,objEVENT,strRECORDID,objCOLUMN,strNEWVALUE) {
					var server = tibco.admin.Main.getServer();
					var objCDFDocument = server.getCache().getDocument(server.getJSXByName("mtxDetail_56L").getXMLId());
					var objRec = objCDFDocument.getRecordNode(strRECORDID);
					if(objRec) {
						if(strNEWVALUE == "" || strNEWVALUE == null || strNEWVALUE == undefined){
							objRec.setAttribute("isToSave", "false");
						}else{
							if(objRec.getAttribute("isNewNode") == "true"){
								objRec.setAttribute(objCOLUMN.getText(), "true");
								if(objRec.getAttribute(objCOLUMN.getParent().getFirstChild().getText()) == "true" && objRec.getAttribute(objCOLUMN.getParent().getFirstChild().getNextSibling().getText()) == "true" && objRec.getAttribute(objCOLUMN.getParent().getLastChild().getText()) == "true"){
									objRec.setAttribute("isToSave", "true");
								}
							}else{
								objRec.setAttribute("isToSave", "true");
							}
						}
					}
                };

            });
}